var orderCode = UrlParameter().orderCode;

/**格式化数据*/
function formatData(data) {
    /**拣货序号*/
    var orderNumber = data.ticketNum ? data.ticketNum : "";

    /**计算优惠金额*/
    var preferentialAmount = !data.preferentialTotalAmount ? 0.00 : data.preferentialTotalAmount;
    /**支付类型*/
    var orderPayType = data.paymentType !== undefined ? data.paymentType : "";

    /**收货人详细地址 */
    var receiverAddress = data.receiver ? (data.receiver.provinceName === '-' || data.receiver.provinceName === undefined ? "" :data.receiver.provinceName) +
    (data.receiver.cityName === '-' || data.receiver.cityName === undefined ? "" :data.receiver.cityName) +
    (data.receiver.districtName === '-' || data.receiver.districtName === undefined ? "" :data.receiver.districtName) + (data.receiver.detailAddress) : "";

    var result = [
    //   {
    //     "con": "益丰大药房",
    //     "sy": { "tp": "1", "fs": "3", "gy": "2", "st": "1", "nl": "1" }

    // }, {
    //     "con": (data.topChannelName != undefined ? data.topChannelName : '') + " #" + orderNumber + " 拣货单",
    //     "sy": { "tp": "1", "fs": "3", "gy": "2", "st": "1", "nl": "1" }
    // }, {
    //     "con": "(商家留存)",
    //     "sy": { "tp": "1", "fs": "2", "gy": "2", "st": "1", "nl": "1" }
    // }, {
    //     "con": "订单编号: " + (data.orderCode !== undefined ? data.orderCode : ""),
    //     "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }

    // }
    ];

    // if (data.outTradeCode) {
    //     result.push({
    //         "con": "交易编号: " + data.outTradeCode,
    //         "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //     });
    // }

    // result.push(
    //     {
    //         "con": "下单时间: " + (data.createTime !== undefined ? data.createTime : ""),
    //         "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //     },
    //     {
    //         "con": "支付方式: " + (orderPayType !== undefined ? orderPayType : ""),
    //         "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //     }
    // );

    // if (data.needInvoice) {  //没有发票抬头 不显示
    //     result.push({
    //         "con": "是否开发票: " + (data.needInvoice ? "是" : "否"),
    //         "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //     }, {
    //             "con": "发票抬头: " + (data.invoiceTitle !== undefined ? data.invoiceTitle : "无"),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         });
    // } else {
    //     result.push({
    //         "con": "是否开发票: 否",
    //         "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //     });
    // }

    // result.push({
    //     "con": "---",
    //     "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    // }, {
    //         "con": "  品名   数量   单价   金额  ",
    //         "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }
    //     }, {
    //         "con": "---",
    //         "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //     });


    var goodsTotalNumber = 0;
    var refundGoodsTotalAmount = 0;
    // if(data.isRefund == "Y"){
    //     $.each(data.refundGoodsList, function (i, item) {
    //         /**商品信息*/
    //         result.push({
    //             "con": i + 1 + " " + (item.goodsName !== undefined ? item.goodsName : "") + (item.gift ? " （赠品）" : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         });
    //         /**69码展示*/
    //         result.push({
    //             "con": "69码: " + (item.mainBarcode !== undefined ? item.mainBarcode : ""),
    //             "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
    //         });
    //         /**商品数量 、单价、商品合计*/
    //         result.push({
    //             "con": "            (x" + (item.quantity !== undefined ? item.quantity : "") + ")     " + (item.exercisePrice !== undefined ? item.exercisePrice : "") + "    " + (item.exerciseAmount !== undefined ? item.exerciseAmount : ""),
    //             "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
    //         });
    //         /**商品总件数*/
    //         goodsTotalNumber += parseInt(item.quantity !== undefined ? item.quantity : 0);
    //         /** 商品执行价 */
    //         refundGoodsTotalAmount += parseFloat(item.quantity * item.exercisePrice !== undefined ? item.quantity*item.exercisePrice: 0);
    //     });
    //     result.push(
    //         {
    //             "con": "---",
    //             "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }, {
    //             "con": "商品金额: " + (refundGoodsTotalAmount !== undefined ? refundGoodsTotalAmount : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }
    //     );
    // }else{
    //     if (data.listGoods) {
    //         $.each(data.listGoods, function (i, item) {
    //             /**商品信息*/
    //             result.push({
    //                 "con": i + 1 + " " + (item.goodsName !== undefined ? item.goodsName : "") + (item.gift ? " （赠品）" : ""),
    //                 "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //             });
    //             /**69码展示*/
    //             result.push({
    //                 "con": "69码: " + (item.mainBarcode !== undefined ? item.mainBarcode : ""),
    //                 "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
    //             });
    //             /**商品数量 、单价、商品合计*/
    //             result.push({
    //                 "con": "            (x" + (item.quantity !== undefined ? item.quantity : "") + ")     " + (item.exercisePrice !== undefined ? item.exercisePrice : "") + "    " + (item.exerciseAmount !== undefined ? item.exerciseAmount : ""),
    //                 "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
    //             });
    //             /**商品总件数*/
    //             goodsTotalNumber += parseInt(item.quantity !== undefined ? item.quantity : 0);
    //         });
    //     }
    //     result.push(
    //         {
    //             "con": "---",
    //             "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }, {
    //             "con": "商品金额: " + (data.goodsTotalAmount !== undefined ? data.goodsTotalAmount : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }
    //     );
    // }


    // if (data.additionalInfos) {
    //     $.each(data.additionalInfos, function (i, item) {
    //         /**积分*/
    //         if(item.infoCode === 'barterPoints'){
    //             result.push(
    //                 {
    //                     "con": "积分: -" + (item.infoValue !== undefined ? item.infoValue : ""),
    //                     "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //                 }
    //             )
    //         }
    //     });

    // }

    // if(data.isRefundFreight === 'Y'){
    //     result.push(
    //         {
    //             "con": "优惠金额: " + preferentialAmount,
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }, {
    //             "con": "运费金额: " + ("0"),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }
    //     );
    // }else{
    //     result.push(
    //         {
    //             "con": "优惠金额: " + preferentialAmount,
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }, {
    //             "con": "运费金额: " + (data.deliveryAmount !== undefined ? data.deliveryAmount : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }
    //     );
    // }

    // if (data.packingAmount && data.packingAmount > 0) {
    //     result.push(
    //         {
    //             "con": "包装费: " + (data.packingAmount !== undefined ? data.packingAmount : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }
    //     )
    // }

    // if(data.isRefund == "Y"){
    //     result.push(
    //         {
    //             "con": "总件数: " + goodsTotalNumber,
    //             "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
    //         }, {
    //             "con": "备注: " + (data.remark !== undefined ? data.remark : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }, {
    //             "con": "总计金额: " + (data.customerNeedPayAmount - data.refundAmount !== undefined ? (data.customerNeedPayAmount*1000 - data.refundAmount*1000) /1000 : 0.00),
    //             "sy": { "tp": "1", "fs": "3", "gy": "3", "st": "1", "nl": "1" }
    //         }
    //     );
    // }else{
    //     result.push(
    //         {
    //             "con": "总件数: " + goodsTotalNumber,
    //             "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
    //         }, {
    //             "con": "备注: " + (data.remark !== undefined ? data.remark : ""),
    //             "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    //         }, {
    //             "con": "总计金额: " + (data.customerNeedPayAmount !== undefined ? data.customerNeedPayAmount : 0.00),
    //             "sy": { "tp": "1", "fs": "3", "gy": "3", "st": "1", "nl": "1" }
    //         }
    //     );
    // }

    // result.push({
    //     "con": "---",
    //     "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "2" }
    // });

    result.push(
        {
        "con": "益丰大药房",
        "sy": { "tp": "1", "fs": "3", "gy": "2", "st": "1", "nl": "1" }
        },
        {
            "con": (data.topChannelName != undefined ? data.topChannelName : '') + " #" + orderNumber + " 客户联",
            "sy": { "tp": "1", "fs": "3", "gy": "2", "st": "1", "nl": "1" }
        },
        {
            "con": (data.warehouseName),
            "sy": { "tp": "1", "fs": "1", "gy": "2", "st": "1", "nl": "1" }
        },
        {
            "con": (data.channelName !== undefined ? data.channelName : ""),
            "sy": { "tp": "1", "fs": "1", "gy": "2", "st": "1", "nl": "1" }
        },  {
            "con": "订单号: " + (data.orderCode? data.orderCode : ''),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        },
        {
            "con":   (data.outTradeCode ? "三方单号: "+ data.outTradeCode : ''),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        },
        {
            "con": "支付方式: " + (orderPayType !== undefined ? orderPayType : ""),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        },
        {
            "con": "是否开发票: " + (data.needInvoice ? "是" : "否"),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        });
        if(data.needInvoice){
            result.push(
            {
                "con": "发票抬头: " + (data.invoiceTitle !== undefined && data.invoiceTitle !== '' ? data.invoiceTitle : "无"),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
            ,
            {
                "con": "纳税人识别号: " + (data.invoiceCode !== undefined && data.invoiceCode !== '' ? data.invoiceCode : "无"),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            });
        }
        result.push(
        {
            "con": "门店地址: " + (data.warehouseAddress !== undefined ? data.warehouseAddress : "渠道上没有配置门店地址"),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        }, {
            "con": "【温馨提示】此商品属于特殊商品，非质量问题概不退换",
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        }, {
            "con": "---",
            "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        },
        {

                "con": "品名   "+(data.showLotNo !== undefined && data.showLotNo === 'Y' ? "批号   数量   单价   金额" : "数量   单价   金额"),
                "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }

        }
        ,
        {
            "con": "---",
            "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        });

    if(data.isRefund == "Y"){
            if (data.refundGoodsList)
                $.each(data.refundGoodsList, function (i, item) {
                    result.push({
                        "con": i + 1 + " " + (item.goodsName !== undefined ? item.goodsName : "") + "  " +(item.lotNo !== undefined ? item.lotNo : "") + (item.gift ? "(赠品)" : ""),
                        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                    },
                    /**69码展示*/
                    {
                        "con": "69码: " + (item.mainBarcode !== undefined ? item.mainBarcode : ""),
                        "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
                    },
                    {
                        "con": "            (x" + (item.quantity !== undefined ? item.quantity : "") + ")     " + (item.exercisePrice !== undefined ? item.exercisePrice : "") + "    " + (item.exerciseAmount !== undefined ? item.exerciseAmount : ""),
                        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                    },
                    {
                        "con": (item.dosage !== undefined ? "用法用量: " + item.dosage : ""),
                        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                    });
                    if (item.drugManageTypeCode === '5' || item.drugManageTypeCode === '6') {
                        result.push({
                            "con": "运动员慎用!",
                            "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
                        });
                    }
                     /**商品总件数*/
                    goodsTotalNumber += parseFloat(item.quantity !== undefined ? item.quantity : 0);
                    /** 商品执行价 */
                    refundGoodsTotalAmount += parseFloat(item.quantity * item.exercisePrice !== undefined ? item.quantity*item.exercisePrice: 0);
            });
    }else{
        if (data.listGoods)
            $.each(data.listGoods, function (i, item) {
                result.push({
                    "con": i + 1 + " " + (item.goodsName !== undefined ? item.goodsName : "") + "  " +(item.lotNo !== undefined ? item.lotNo : "") + (item.gift ? "(赠品)" : ""),
                    "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                },
                /**69码展示*/
                {
                    "con": "69码: " + (item.mainBarcode !== undefined ? item.mainBarcode : ""),
                    "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
                },
                {
                    "con": "            (x" + (item.quantity !== undefined ? item.quantity : "") + ")     " + (item.exercisePrice !== undefined ? item.exercisePrice : "") + "    " + (item.exerciseAmount !== undefined ? item.exerciseAmount : ""),
                    "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
                },{
                    "con": (item.dosage !== undefined ? "用法用量: " + item.dosage : ""),
                    "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                });
                if (item.drugManageTypeCode === '5' || item.drugManageTypeCode === '6') {
                    result.push({
                        "con": "运动员慎用!",
                        "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "2", "nl": "1" }
                    });
                }
                  /**商品总件数*/
                  goodsTotalNumber += parseFloat(item.quantity !== undefined ? item.quantity : 0);

                  refundGoodsTotalAmount += parseFloat(item.quantity * item.exercisePrice !== undefined ? item.quantity*item.exercisePrice: 0);
            });
    }

    result.push(
        {
            "con": "---",
            "sy": { "tp": "3", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        }, {
            "con": "商品金额: " + (refundGoodsTotalAmount !== undefined ? refundGoodsTotalAmount.toFixed(2) : ""),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        }
    );

    if (data.additionalInfos) {
        $.each(data.additionalInfos, function (i, item) {
            /**积分*/
            if(item.infoCode === 'barterPoints'){
                result.push(
                    {
                        "con": "积分: -" + (item.infoValue !== undefined ? item.infoValue : ""),
                        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                    }
                )
            }
        });

    }


    if(data.deliveryProvider ==='DELIVERY_THIRD') {
        result.push(
            {
                "con": "优惠金额: " + preferentialAmount,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }, {
                "con": "运费金额: " + (data.deliveryAmount !== undefined ? data.deliveryAmount : 0),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
        );
        refundGoodsTotalAmount += ((data.deliveryAmount !== undefined ? data.deliveryAmount : 0) - (preferentialAmount !== undefined?preferentialAmount:0));
    }else if(data.isRefundFreight === 'Y'){
        result.push(
            {
                "con": "优惠金额: " + preferentialAmount,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }, {
                "con": "运费金额: " + ("0"),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
        );
        refundGoodsTotalAmount -= (preferentialAmount !== undefined?preferentialAmount:0);
    }else{
        result.push(
            {
                "con": "优惠金额: " + preferentialAmount,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }, {
                "con": "运费金额: " + (data.deliveryAmount !== undefined ? data.deliveryAmount : 0),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
        );
        refundGoodsTotalAmount += ((data.deliveryAmount !== undefined ? data.deliveryAmount : 0) -(preferentialAmount !== undefined?preferentialAmount:0)) ;
        //refundGoodsTotalAmount += (data.deliveryAmount !== undefined ? data.deliveryAmount : 0);
    }

    result.push(
        {
            "con": "包装费: " + (data.packingAmount !== undefined ? data.packingAmount : 0),
            "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
        }
    )
    refundGoodsTotalAmount += (data.packingAmount !== undefined ? data.packingAmount : 0);
    if(data.isRefund == "Y"){
        result.push(
            {
                "con": "总件数: " + goodsTotalNumber,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "2", "nl": "1" }
            },
            {
                "con": (data.remark !== undefined && data.remark !== ''  ? "备注: " + data.remark : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            },
            {
                "con": "总计金额: " + refundGoodsTotalAmount.toFixed(2),
                "sy": { "tp": "1", "fs": "3", "gy": "1", "st": "1", "nl": "1" }
            }
        );
    }else{
        result.push(
            {
                "con": "总件数: " + goodsTotalNumber,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "2", "nl": "1" }
            },
            {
                "con": (data.remark !== undefined && data.remark !== ''? "备注: " + data.remark : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            },
            {
                "con": "总计金额: " + refundGoodsTotalAmount.toFixed(2) ,
                "sy": { "tp": "1", "fs": "3", "gy": "1", "st": "1", "nl": "1" }
            }
        );
    }
    if(data.topChannelCode === 'ZY') {
        result.push( {
            "con": "门店电话：400-921-6690",
            "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }
        });
        result.push( {
            "con": "总部投诉电话：400-921-6690",
            "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }
        });
    } else {
        result.push( {
            "con": "门店电话："+data.warehousePhone,
            "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }
        });
        result.push( {
            "con": "总部投诉电话：400-015-6199",
            "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }
        });
    }

    /**渠道提醒*/
    if(data.channelRemark){
    	 result.push( {
	        "con": data.channelRemark,
	        "sy": { "tp": "1", "fs": "2", "gy": "1", "st": "1", "nl": "1" }
	    });
    }



    result.push(
    {
        "con": "-----------裁----剪----线-------------",
        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "3" }
    });

    if (data.deliveryType === '实时' || data.deliveryType === '自提' || data.deliveryType === '普通') {
        result.push(
            {
                "con": "益丰大药房",
                "sy": { "tp": "1", "fs": "3", "gy": "2", "st": "1", "nl": "1" }
            },
            {
                "con": (data.topChannelName != undefined ? data.topChannelName : '') + " #" + orderNumber + " 客户联",
                "sy": { "tp": "1", "fs": "3", "gy": "2", "st": "1", "nl": "1" }
            },
            {
                "con": (data.warehouseName),
                "sy": { "tp": "1", "fs": "1", "gy": "2", "st": "1", "nl": "1" }
            },
            {
                "con": (data.channelName !== undefined ? data.channelName : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "2", "st": "1", "nl": "2" }
            }
            );
    }

    result.push(
        {
            "con": data.outTradeCode ? data.outTradeCode : data.orderCode,
            "sy": { "tp": "2", "fs": "1", "gy": "3", "st": "1", "nl": "1" }
        }
    );
    /*添加预约单的送达时间*/
    if (data.additionalInfos) {
        $.each(data.additionalInfos, function (i, item) {
            /**预约单的送达时间*/
            if(item.infoCode === 'reservedDeliveryTime'){
                result.push(
                    {
                        "con": "预约单的送达时间: " + (item.infoValue !== undefined ? item.infoValue : ""),
                        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
                    }
                )
            }
        });

    }
    if(data.topChannelCode === 'XINGREN' || data.topChannelCode === 'CYYS' || data.topChannelCode === 'CYYSWX' || data.topChannelCode === 'LHYS'){
        var receiverPhone = data.receiver !== undefined ? data.receiver.phone : "";
        if (receiverPhone !== '') {
            receiverPhone = receiverPhone.substr(0, 3) + '****' + receiverPhone.substr(7);
        }
        result.push(
            {
                "con": "收货人姓名: " + (data.receiver !== undefined ? data.receiver.name : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            },
            {
                "con": "收货人电话: " + (receiverPhone),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            },
            {
                "con": "收货人地址: " + receiverAddress,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
        );
    } else {
        result.push(
            {
                "con": "收货人姓名: " + (data.receiver !== undefined ? data.receiver.name : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            },
            {
                "con": "收货人电话: " + (data.receiver !== undefined ? data.receiver.phone : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            },
            {
                "con": "收货人地址: " + receiverAddress,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
        );
    }
    if (data.deliveryType === '实时' || data.deliveryType === '自提') {
        result.push(
            {
                "con": (data.remark !== undefined && data.remark !== ''  ? "备注: " + data.remark : ""),
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            }
        );
    }
    if(data.topChannelCode === 'XRQEYS'){
        result.push(
            {
                "con": "应收金额: " + data.goodsTotalAmount,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            });
    }else{
        result.push(
            {
                "con": "应收金额: " + 0,
                "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
            });
    }
    result.push(
    {
        "con": "客户签名: ",
        "sy": { "tp": "1", "fs": "1", "gy": "1", "st": "1", "nl": "1" }
    });

    var arr = [];
    arr.push(result);
    return arr;
}



//创建小票
function createPrintTemplate(dataList) {
    var rowDataList = dataList;
    var len = rowDataList.length;
    var template = [];

    for (var j = 0; j < len; j++) {
        var rowData = rowDataList[j];
        var rows = rowDataList[j].length;

        for (var i = 0; i < rows; i++) {
            var obj = rowData[i];
            var text = obj.con;

            text = text ? text.replace(/\ /g, '<span class="space">&nbsp;</span>') : "";

            var style = obj.sy;
            var tp = style.tp;	//类型
            var fs = style.fs;	//字号
            var st = style.st;	//是否加粗，新增
            var gy = style.gy;	//对齐
            var nl = style.nl;	//空行数，新增

            //内容
            if (tp == 1) {
                template[template.length] = '<div class="print-item';

                if (fs == 1) {
                    template[template.length] = ' f-s';
                } else if (fs == 2) {
                    template[template.length] = ' f-m';
                } else if (fs == 3) {
                    template[template.length] = ' f-l';
                }

                if (gy == 1) {
                    template[template.length] = ' tl';
                } else if (gy == 2) {
                    template[template.length] = ' tc';
                } else if (gy == 3) {
                    template[template.length] = ' tr';
                }

                //是否加粗
                if (st == 2) {
                    template[template.length] = ' bold';
                }

                template[template.length] = '">';
                template[template.length] = text;
                template[template.length] = '</div>';

                //空行数量
                for (var k = 0; k < nl - 1; k++) {
                    template[template.length] = '<p class="br"></p>';
                }
            }

            //条形码
            if (tp == 2) {
                template[template.length] = '<div class="tc"><div class="print-codeline" data-id="';
                template[template.length] = text;
                template[template.length] = '"></div></div>';

                //空行数量
                for (var k = 0; k < nl - 1; k++) {
                    template[template.length] = '<p class="br"></p>';
                }
            }

            //分割线
            if (tp == 3) {
                template[template.length] = '<div class="dashed"></div>';
                //空行数量
                for (var k = 0; k < nl - 1; k++) {
                    template[template.length] = '<p class="br"></p>';
                }
            }

            //二维码
            if (tp == 4) {
                template[template.length] = '<div class="tc"><div class="print-qrcode" data-id="';
                template[template.length] = text;
                template[template.length] = '"></div></div>';

                //空行数量
                for (var k = 0; k < nl - 1; k++) {
                    template[template.length] = '<p class="br"></p>';
                }
            }
          //分割线
            if (tp == 5) {
                template[template.length] = '<div class="cutting-line"></div>';
                //空行数量
                for (var k = 0; k < nl - 1; k++) {
                    template[template.length] = '<p class="br"></p>';
                }
            }

        }
    }

    //插入小票模板
    $("#printTemplate").append(template.join('') + '<p class="br"></p><p class="br"></p><div class="dashed"></div>').append('<br/><br/><br/>');

    //生成条形码
    $(".print-codeline").each(function () {
        var codeNo = $(this).attr("data-id");
        $(this).empty().barcode(codeNo, "code128", { barWidth: 1, barHeight: 60, showHRI: true });
    });
   $(".print-codeline :first").remove();
   $(".print-codeline :last").css({'text-align':'left'});
    //生成二维码
    $(".print-qrcode").each(function () {
        var url = $(this).attr("data-id");
        var qrCodeParent = $(this);
        //生成canvas
        qrCodeParent.qrcode({ text: url, width: 100, height: 100, correctLevel: 1 });
        //插入img
        qrCodeParent.find("canvas").each(function () {
            var oImg = Canvas2Image.saveAsBMP(this, true);
            qrCodeParent.html(oImg);
        });

    });

}


//执行打印
$("#printStart").bind("click", function () {
    var self = $(this);
    self.hide();//隐藏按钮
    setTimeout(function () {
        window.print();
        setTimeout(function () {
            self.show();
        }, 500);
    }, 500);
});


/**打印销售小票*/
$(function () {
    $("#printStart").hide();
    var channelMap = new Map();
    $.ajaxSetup({ async: false });
    $X("/boss/channelService", "listAllTopChannel").callx({
        onResult: function (result) {
            for (var i = 0; i < result.length; i++) {
                channelMap.put(result[i].channelCode, result[i].name);
            }
        },
        onError: function (error) {
        }
    });

    //获取订单数据
    $X("/order/queryService", "printTicket").callx({
        orderCode: orderCode,
        onResult: function (data) {
            try {
                if (data) {
                    for (var i = 0; i < data.length; i++) {
                        data[i].topChannelName = channelMap.get(data[i].topChannelCode);
                        data[i].createTime = new Date(data[i].createTime).format("yyyy-MM-dd hh:mm:ss")

                        createPrintTemplate(formatData(data[i]));
                    }
                    layer.closeAll("loading")
                    $("#printStart").show();

                }
            } catch (e) { }
        },
        onError: function (xhr, status, error) {
            var message = $.parseJSON(xhr.responseText);
            layer.msg(message);
        }
    });
});
